
package com.yjh.admin.server.sys.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yjh.admin.config.data.DataSource;
import com.yjh.admin.dto.user.UserDto;
import com.yjh.admin.entity.user.SysRole;
import com.yjh.admin.mapper.user.SysUserMapper;
import com.yjh.admin.server.sys.SysMenuService;
import com.yjh.admin.server.sys.SysRoleService;
import com.yjh.admin.server.sys.SysUserService;
import enums.MenuVO;
import enums.SysUser;
import enums.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户表
 *
 * @author smithGuo
 * @date 2020-07-01 08:38:57
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 通过查用户的全部信息
     *
     * @param sysUser 用户
     * @return
     */
    @Override
    public UserInfo findUserInfo(SysUser sysUser) {
        UserInfo userInfo = new UserInfo();
        userInfo.setSysUser(sysUser);
        //设置角色列表  （ID）
        List<Integer> roleIds = sysRoleService.findRolesByUserId(sysUser.getUserId())
                .stream()
                .map(SysRole::getRoleId)
                .collect(Collectors.toList());
        userInfo.setRoles(ArrayUtil.toArray(roleIds, Integer.class));

        //设置权限列表（menu.permission）
        Set<String> permissions = new HashSet<>();
        for (Integer roleId:roleIds) {
            List<String> permissionList = sysMenuService.findMenuByRoleId(roleId).stream()
                    .filter(menuVo -> StringUtils.isNotEmpty(menuVo.getPermission()))
                    .map(MenuVO::getPermission)
                    .collect(Collectors.toList());
            permissions.addAll(permissionList);
        }


		/*roleIds.forEach(roleId -> {
			List<String> permissionList = sysMenuService.findMenuByRoleId(roleId,sysUser.getOwnPlatform())
					.stream()
					.filter(menuVo -> StringUtils.isNotEmpty(menuVo.getPermission()))
					.map(MenuVO::getPermission)
					.collect(Collectors.toList());

		});*/
        userInfo.setPermissions(ArrayUtil.toArray(permissions, String.class));
        return userInfo;
    }


    @Override
    @DataSource("sys_db")
    public SysUser getByName(String userName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",userName);
        SysUser user = baseMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    @DataSource("sys_db")
    public SysUser getById(Integer userId) {
        return baseMapper.selectById(userId);
    }

    @Override
    @DataSource("sys_db")
    public List<UserDto> queryUser() {
        List<UserDto> list = baseMapper.queryUser();
        return list;
    }

    @Override
    @DataSource("sys_db")
    public int saveSysUser(SysUser sysUser) {
        return baseMapper.insert(sysUser);
    }
}
